# -*- coding: UTF-8 -*- #
"""
@filename:03-2色彩空间.py
@author:JiYujia
@time:2022-09-05
"""
# 算法 hsv
# 人 rgb
import cv2 as cv
import numpy as np


# 将图片安装数组形式排列
def stackImages(scale, imgArray):
    rows = len(imgArray)
    cols = len(imgArray[0])
    rowsAvailable = isinstance(imgArray[0], list)
    width = imgArray[0][0].shape[1]
    height = imgArray[0][0].shape[0]
    if rowsAvailable:
        for x in range(0, rows):
            for y in range(0, cols):
                if imgArray[x][y].shape[:2] == imgArray[0][0].shape[:2]:
                    imgArray[x][y] = cv.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None,
                                               scale, scale)
                if len(imgArray[x][y].shape) == 2:
                    imgArray[x][y] = cv.cvtColor(imgArray[x][y], cv.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height, width, 3), np.uint8)
        hor = [imageBlank] * rows
        hor_con = [imageBlank] * rows
        for x in range(0, rows):
            hor[x] = np.hstack(imgArray[x])
        ver = np.vstack(hor)
    else:
        for x in range(0, rows):
            if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                imgArray[x] = cv.resize(imgArray[x], (0, 0), None, scale, scale)
            else:
                imgArray[x] = cv.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None, scale, scale)
            if len(imgArray[x].shape) == 2: imgArray[x] = cv.cvtColor(imgArray[x], cv.COLOR_GRAY2BGR)
        hor = np.hstack(imgArray)
        ver = hor
    return ver

def empty():
    pass

img_path = 'pics/car.png'

# 创建一个窗体
cv.namedWindow('TrackBars')
cv.resizeWindow('TrackBars', 640, 240)

# 创建6个滑动条 hsv
cv.createTrackbar('Hue Min', 'TrackBars', 0, 180, empty)
cv.createTrackbar('Hue Max', 'TrackBars', 180, 180, empty)
cv.createTrackbar('Sat Min', 'TrackBars', 139, 255, empty)
cv.createTrackbar('Sat Max', 'TrackBars', 255, 255, empty)
cv.createTrackbar('Val Min', 'TrackBars', 29, 255, empty)
cv.createTrackbar('Val Max', 'TrackBars', 255, 255, empty)

# 读入图像
img = cv.imread(img_path)

# 色彩空间变换 rgb-》hsv
imgHSV = cv.cvtColor(img, cv.COLOR_BGR2HSV)

# 颜色分割 - 循环：获取滑动条的数值
while True:
    h_min = cv.getTrackbarPos('Hue Min', 'TrackBars')
    h_max = cv.getTrackbarPos('Hue Max', 'TrackBars')
    s_min = cv.getTrackbarPos('Sat Min', 'TrackBars')
    s_max = cv.getTrackbarPos('Sat Max', 'TrackBars')
    v_min = cv.getTrackbarPos('Val Min', 'TrackBars')
    v_max = cv.getTrackbarPos('Val Max', 'TrackBars')

    print()

    # 颜色分割
    lower = np.array([[h_min], [s_min], [v_min]])
    upper = np.array([[h_max], [s_max], [v_max]])

    mask = cv.inRange(imgHSV, lower, upper)

    imgResult = cv.bitwise_and(img, img, mask=mask)

    # 输出显示，获取ROI区域后对应显示和调试
    # cv.imshow('src', img)
    # cv.imshow('HSV', imgHSV)
    # cv.imshow('mask', mask)
    # cv.imshow('result', imgResult)

    imgStack = stackImages(0.6, ([img, imgHSV], [mask, imgResult]))
    cv.imshow('stack image', imgStack)

    if cv.waitKey(1) & 0xFF == ord('q'):
        break

cv.destroyAllWindows()
